package com.zjitc.controller;

import com.zjitc.model.Country;
import com.zjitc.service.CountryService;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.List;

/**
 * Created by Administrator on 2018/3/20.
 */
@Controller
public class CountryController {
    @Autowired
    private CountryService countryService;

    @RequestMapping("/findAllCountry")
    public ModelAndView findAll(){
        List<Country> listCountrys = countryService.findAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("countrys",listCountrys);
        mv.setViewName("/WEB-INF/views/countrys.jsp");
        return mv;
    }

    @RequestMapping("/addAll")
    public View addAll(){
        View view = new InternalResourceView("/WEB-INF/views/addCountry.jsp");
        return view;
    }

    @RequestMapping("/addCountry")
    public View add(HttpServletRequest request){
      Country country = new Country();
      try {
        BeanUtils.populate(country,request.getParameterMap());
        int num =countryService.add(country);
        System.out.println(num);
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
      View view = new InternalResourceView("/findAllCountry.do");
      return view;
    }

    @RequestMapping("/deleteCountry")
    public View delete(HttpServletRequest request){
      Country country = new Country();
      try {
        BeanUtils.populate(country,request.getParameterMap());
        int num = countryService.delete(country.getName());
        System.out.println(num);
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
      View view = new InternalResourceView("/findAllCountry.do");
      return view;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest request){
      String name = request.getParameter("name");
      Country country = countryService.edit(name);
      ModelAndView mv = new ModelAndView();
      mv.addObject("countrys",country);
      mv.setViewName("/WEB-INF/views/updateCountry.jsp");
      return mv;
    }

    @RequestMapping("/updateCountry")
    public View update(HttpServletRequest request){
      Country country = new Country();
      try {
        BeanUtils.populate(country,request.getParameterMap());
        int num = countryService.update(country);
        System.out.println(num);
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }

      View view = new InternalResourceView("/findAllCountry.do");
      return view;
    }

    @RequestMapping("/findAllCate")
    public ModelAndView findAllCate(HttpServletRequest request){
      Country country = new Country();
      try {
        BeanUtils.populate(country,request.getParameterMap());
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
      List<Country> listCountrys = countryService.findAllCate();
      ModelAndView mv = new ModelAndView();
      mv.addObject("countrys",listCountrys);
      mv.setViewName("/WEB-INF/views/countrys.jsp");
      return mv;
    }

}
